import 'package:fish_redux/fish_redux.dart';
import 'package:fish_redux/src/redux/basic.dart' as fish;
import 'action.dart';
import 'state.dart';

import 'package:http/http.dart' as http;
import 'dart:convert';
import 'dart:io'; // 网络请求

import 'package:flutter/material.dart';
import 'package:rss_readneed/punch_card/setting/page.dart' as setting;
import 'package:rss_readneed/public.dart';

import 'package:rss_readneed/sn_support/SNAccount.dart';

Effect<sn_supportState> buildEffect() {
  return combineEffects(<Object, Effect<sn_supportState>>{
    Lifecycle.initState:_init,
    Lifecycle.dispose:_dispose,
    sn_supportAction.loginAction: _loginAction,
  });
}

void _init(fish.Action action, Context<sn_supportState> ctx) async {

  SNAccount account = await SNAccount().getSNAccount();

  if (account.account != null && account.account.length != 0) {

    ctx.state.nameController.text = account.account;

    ctx.state.isNeedRecommend = false;

    //初始有账号，那么更改账号会重新比对是否需要推荐码
    ctx.state.nameController.addListener((){

      if (ctx.state.nameController.text != account.account) {

        ctx.state.isNeedRecommend = true;
      } else {

        ctx.state.isNeedRecommend = false;
      }
      //刷新
      ctx.dispatch(sn_supportActionCreator.onAction());
    });
  } else {

    ctx.state.isNeedRecommend = true;
  }

  //刷新
  ctx.dispatch(sn_supportActionCreator.onAction());
}

void _dispose(fish.Action action, Context<sn_supportState> ctx) {

  ctx.state.nameController.dispose();
  ctx.state.passController.dispose();
  ctx.state.recommendController.dispose();
}

void _loginAction(fish.Action action, Context<sn_supportState> ctx) async {

  String monthTime;
//
//  http.Response response = await http.get("http://10.46.79.160:8888/queryAllArticle");
//
//  print(response.body);

  String cookies = "authId=si3355FB16277A71A9F6016402ED6EC384; jira_user_no=88418273; route=e49a9edfb74d1d762a8f41eb25bcc98b; idsLoginUserIdLastTime=88418273; isso_ld=true; isso_us=88418273";

  List checkTimes = await SNAccount().fetchListDatas(ctx.state.nameController.text, cookies,(msg){

    Toast.show(ctx.context, msg);
  },true);

  if (checkTimes.length > 0) {

    monthTime = checkTimes.last.substring(0,7);

    showDialog<Null>(
      context: ctx.context,
      barrierDismissible: true,
      builder: (BuildContext context) {
        return new AlertDialog(
          title: new Text('获取成功'),
          actions: <Widget>[
            RaisedButton(
              onPressed: (){ AppNavigator.pop(context); AppNavigator.pop(context);/*回退上一个页面*/ },
              child: Text("取消"),
              textColor: Colors.black54,
              color: Colors.white,
              shape: StadiumBorder(side: BorderSide(color: Colors.black38)),
//                shape: ContinuousRectangleBorder(borderRadius:BorderRadius.all(Radius.circular(5)) ),
            ),
            RaisedButton(
              onPressed: (){

                if (SNAccount.instance.isSnPageFirst == true) {//如果是sn的主页直接回主页查看即可

                  AppNavigator.pop(context); AppNavigator.pop(context);/*回退上一个页面*/

                } else {//非sn主页去设置页面查看，可设置

                  AppNavigator.pop(context); AppNavigator.push(ctx.context, setting.punch_card_settingPage().buildPage({"monthTime":monthTime}));
                }
              },
              child: Text("查看"),
              textColor: Colors.white,
              color: Colors.red,
              shape: StadiumBorder(),
//                shape: ContinuousRectangleBorder(borderRadius:BorderRadius.all(Radius.circular(5)) ),
            )
          ],
        );
      },
    );
  }
}



